/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.composedui.views;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;

import com.nokia.mid.ui.LCDUIUtil;

import com.nokia.example.composedui.ViewManager;
import com.nokia.example.composedui.model.DataItem;
import com.nokia.example.composedui.model.ItemStorage;
import com.nokia.example.composedui.model.ItemStorageListener;
import com.nokia.example.composedui.utils.AppDataStorage;
import com.nokia.example.composedui.utils.PersistableState;
import com.nokia.example.composedui.views.elements.GridLayout;
import com.nokia.example.composedui.views.elements.IconListItem;

/**
 * This view displays the items so that the first three items are in a single
 * line row layout and the rest of the items are aligned below as a list with
 * icons.
 */
public class SplitView
    extends Form
    implements CommandListener,
               ItemStateListener,
               ItemStorageListener,
               PersistableState
{
    // Constants
    private static final int ITEM_COUNT_IN_GRID_LAYOUT = 3;
    private static final int LIST_ROW_HEIGHT = 40;

    // Members
    protected ViewManager viewManager;
    protected ItemStorage itemStorage;
    protected GridLayout gridLayout;

    private final Command addCommand;
    private final Command deleteMultipleCommand;
    private final Command exitCommand;

    /**
     * Constructor.
     */
    public SplitView() {
        super("Split view");
        
        // Hide the open keyboard command in full touch devices
        final String keyboardType = System.getProperty("com.nokia.keyboard.type");
        
        if (keyboardType.equals("OnekeyBack") || keyboardType.equals("None")) {
            com.nokia.mid.ui.VirtualKeyboard.hideOpenKeypadCommand(true);
        }
        
        gridLayout = new GridLayout(getWidth());
        LCDUIUtil.setObjectTrait(gridLayout, "nokia.ui.s40.item.direct_touch", Boolean.TRUE);
        setItemStateListener(this);
        append(gridLayout);
        
        addCommand = new Command("Add new", Command.SCREEN, 1);
        deleteMultipleCommand = new Command("Delete items", Command.SCREEN, 2);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        addCommand(addCommand);
        addCommand(deleteMultipleCommand);
        addCommand(exitCommand);
        setCommandListener(this);
    }

    /**
     * Constructor.
     * @param itemStorage
     * @param viewManager
     */
    public SplitView(ItemStorage itemStorage, ViewManager viewManager) {
        this();
        restoreInstance(null, itemStorage, viewManager, null, null);
    }

    /**
     * @see PersistableState#restoreInstance(DataItem, ItemStorage, ViewManager, AppDataStorage, String)
     */
    public void restoreInstance(DataItem selectedDataItem,
                                ItemStorage itemStorage,
                                ViewManager viewManager,
                                AppDataStorage appDataStorage,
                                String uniqueId)
    {
        this.viewManager = viewManager;
        this.itemStorage = itemStorage;
        this.itemStorage.addItemStorageListener(this);
    }

    /**
     * @see PersistableState#saveInstance(AppDataStorage, String)
     */
    public void saveInstance(AppDataStorage appDataStorage, String uniqueId) {
        // No implementation required
    }

    /**
     * @see ItemStorageListener#dataChanged(Vector)
     */
    public void dataChanged(Vector dataItems) {
        populateList(dataItems);
    }

    /**
     * javax.microedition.lcdui.Displayable#sizeChanged(int, int)
     */
    public void sizeChanged(int w, int h) {
        gridLayout.setWidth(w);
    }

    /**
     * @see javax.microedition.lcdui.CommandListener#commandAction(Command, Displayable)
     */
    public void commandAction(Command command, Displayable displayable) {
        if (addCommand.equals(command)) {
            viewManager.showView(new ItemEditView(itemStorage, viewManager));
        }
        else if (deleteMultipleCommand.equals(command)) {
            viewManager.showView(new ItemDeleteView(itemStorage, viewManager));
        }
        else if (exitCommand.equals(command)) {
            viewManager.goBack();
        }
    }

    /**
     * @see javax.microedition.lcdui.ItemStateListener#itemStateChanged(Item)
     */
    public void itemStateChanged(Item item) {
        DataItem dataItem = null;
        
        if (item instanceof GridLayout) {
            dataItem = gridLayout.getSelectedItem();
        }
        else if (item instanceof IconListItem) {
            dataItem = ((IconListItem)item).getDataItem();
        }
        
        if (dataItem != null) {
            viewManager.showView(new ItemView(dataItem, itemStorage, viewManager));
        }
    }

    /**
     * Populates the content of the view.
     * @param dataItems The items to display in the view.
     */
    protected void populateList(Vector dataItems) {
        gridLayout.removeItems();
        deleteAll();
        append(gridLayout);
        Enumeration e = dataItems.elements();
        int index = 0;
        
        while (e.hasMoreElements()) {
            DataItem dataItem = (DataItem)e.nextElement();
            
            if (index < ITEM_COUNT_IN_GRID_LAYOUT) {
                gridLayout.addItem(dataItem);
            }
            else {
                IconListItem iconListItem =
                    new IconListItem(dataItem, getWidth(), LIST_ROW_HEIGHT);
                append(iconListItem);
            }
            
            index++;
        }
    }
}
